<template>
  <div>
    <van-nav-bar title="编辑昵称" left-text="取消" right-text="保存" @click-left="$emit('left-close')" @click-right="onSave" />
    <van-field v-model="name" type="textarea" maxlength="8" placeholder="请输入新昵称" show-word-limit></van-field>
  </div>
</template>

<script>
import { setUserInfos } from '@/api/user.js'
export default {
  data () {
    return {
      name: this.value
    }
  },
  props: {
    value: {
      type: String,
      require: true
    }
  },
  methods: {
    async onSave () {
      if (this.name.trim() === '') return this.$toast('昵称不能为空')
      try {
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true,
          duration: 0
        })
        await setUserInfos({ name: this.name })
        this.$toast('昵称修改成功')
        // 更新视图
        this.$emit('input', this.name)
        // 关闭弹层
        this.$emit('close')
      } catch (err) {
        this.$toast('昵称修改失败')
      }
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {},
  created () { },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
